﻿#region Imports
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using CommonUtilities.Extensions;
using CommonUtilities.Util;
#endregion

namespace RequirementLogger.Models
{
    public class Property
    {
        #region Properties
        public int PropertySort { get; set; }
        public string PropertyName { get; set; }
        public object PropertyValue { get; set; }
        public Type PropertyType { get; set; }
        #endregion

        #region Methods
        public static PropertyCollections CreateProperties<T>(T value) where T : new()
        {
            PropertyCollections res = new PropertyCollections();
            foreach (var prop in typeof(T).GetProperties())
            {
                res.PropertyCollection.Add(new Property
                {
                    PropertyName = prop.Name,
                    PropertyType = prop.PropertyType,
                    PropertyValue = prop.GetValue(value, null)
                });
            }

            return res;
        }

        public static IEnumerable<PropertyCollections> AddProperties<T>(IEnumerable<T> coll) where T : new()
        {
            ObservableCollection<PropertyCollections> trgt = new ObservableCollection<PropertyCollections>();

            //res.ToList().AddRange(
            //    typeof(T).GetProperties().Select(prop => new Property
            //        {
            //            PropertyName = prop.Name,
            //            PropertyType = prop.PropertyType,
            //            PropertyValue = prop.GetValue(prop, null)
            //        }));

            foreach (T t in coll.Cast<T>())
            {
                trgt.Add(CreateProperties<T>(t));
            }

            return trgt.AsEnumerable();
        }
        #endregion
    }

    public class PropertyCollections
    {
        public PropertyCollections()
        {
            PropertyCollection = new ObservableCollection<Property>();
            if (!PropertyCollection.IsNull()) PropertyCollection.CollectionChanged
                   += new NotifyCollectionChangedEventHandler(PropertyCollection_CollectionChanged);
        }

        private ObservableCollection<Property> propertyCollection = new ObservableCollection<Property>();
        public ObservableCollection<Property> PropertyCollection
        {
            get { return propertyCollection; }
            set { propertyCollection = value; }
        }

        public void Clear()
        {
            if (!PropertyCollection.IsNull()) PropertyCollection.Clear();
        }

        private void PropertyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                int lastIndex = PropertyCollection.IsLinqIterable(true)
                    ? PropertyCollection.Select(p => p.PropertySort).Max() : 0; // PropertyCollection.Last().PropertySort
                foreach (Property prop in e.NewItems)
                {
                    lastIndex++;
                    prop.PropertySort = lastIndex;
                }
            }
        }
    }
}
